# 注册 登录
from typing import Optional

from fastapi import APIRouter, Depends
from tortoise.expressions import Q

from core import curd
from core.login import get_current_user
from core.security import get_password_hash
from models import User
from scheams import User_Pydantic, UserIn_Pydantic, SuccessResponse, FailureResponse, BaseResponse

user = APIRouter()


@user.post("/create", summary="增加用户", response_model=BaseResponse)
async def create_user(user_form: UserIn_Pydantic):
    user_form.password = get_password_hash(user_form.password)
    return await curd.create(User, user_form)


@user.delete("/delete", summary="删除用户", response_model=BaseResponse)
async def delete_user(pk: int):
    return await curd.delete(User, pk)


@user.put("/update", summary="更新用户", response_model=BaseResponse)
async def update_user(pk: int, user_form: UserIn_Pydantic):
    return await curd.update(User, user_form, pk)


@user.get("/get", summary="获取用户", response_model=BaseResponse)
async def get_user(pk: int):
    return await curd.read(User, Q(pk=pk), User_Pydantic)


@user.get("/list", summary="获取所有用户", response_model=BaseResponse)
async def get_user_list(limit: Optional[int] = 0, page: Optional[int] = 0):
    return await curd.get_list(User, User_Pydantic, page, limit)


@user.get("/", response_model=BaseResponse)
async def get_user_by_token(current_user: User = Depends(get_current_user)):
    return current_user
